/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.matrimonybeans;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Thanh
 */
@Stateless
public class CandidateDetails implements CandidateDetailsLocal {

    Connection conn;
    String fname;
    String mname;
    String lname;
    String email;
    String birthdate;
    String avatar;
    String gender;
    String maritalStatus;
    int height;
    String country;
    String city;
    String contactNo;
    String motherTongue;
    String religion;
    String caste;
    String selfFeature;
    String family;
    String qualificationCurrentStatus;
    String workAt;
    String designation;
    String location;
    String hobbies;
    String music;
    String movie;
    String cuisinEs;
    String book;
    String place;

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @Override
    public String getFname() {
        return fname;
    }

    @Override
    public String getMname() {
        return mname;
    }

    @Override
    public String getLname() {
        return lname;
    }

    @Override
    public String getEmail() {
        return email;
    }

    @Override
    public String getAvatar() {
        return avatar;
    }

    @Override
    public String getGender() {
        return gender;
    }

    public String getBirthdate() {
        return birthdate;
    }

    @Override
    public String getMaritalStatus() {
        return maritalStatus;
    }

    @Override
    public int getHeight() {
        return height;
    }

    @Override
    public String getCountry() {
        return country;
    }

    @Override
    public String getCity() {
        return city;
    }

    @Override
    public String getContactNo() {
        return contactNo;
    }

    @Override
    public String getMotherTongue() {
        return motherTongue;
    }

    @Override
    public String getReligion() {
        return religion;
    }

    @Override
    public String getCaste() {
        return caste;
    }

    @Override
    public String getSelfFeature() {
        return selfFeature;
    }

    @Override
    public String getFamily() {
        return family;
    }

    @Override
    public String getQualificationCurrentStatus() {
        return qualificationCurrentStatus;
    }

    @Override
    public String getWorkAt() {
        return workAt;
    }

    @Override
    public String getDesignation() {
        return designation;
    }

    @Override
    public String getLocation() {
        return location;
    }

    @Override
    public String getHobbies() {
        return hobbies;
    }

    @Override
    public String getMusic() {
        return music;
    }

    @Override
    public String getMovie() {
        return movie;
    }

    @Override
    public String getCuisinEs() {
        return cuisinEs;
    }

    @Override
    public String getBook() {
        return book;
    }

    @Override
    public String getPlace() {
        return place;
    }

    @Override
    public HashMap viewCandidateById(String username) {
        HashMap viewCandidate = new HashMap();
        ConnectDB connDB = new ConnectDB();


        String sql = "select * from Candidate where username = ?";
        try {
            conn = connDB.getConnection();
            PreparedStatement pstm = conn.prepareStatement(sql);
            pstm.setString(1, username);
            ResultSet rs = pstm.executeQuery();
            while (rs.next()) {
                HashMap hm = new HashMap();
                hm.put("first_name", rs.getString("first_name").trim());
                hm.put("middle_name", rs.getString("middle_name").trim());
                hm.put("last_name", rs.getString("last_name").trim());
                hm.put("avatar", rs.getString("avatar").trim());
                hm.put("gender", rs.getString("gender").trim());
                hm.put("marital_status", rs.getString("marital_status").trim());
                hm.put("country_living_in", rs.getString("country_living_in").trim());
                hm.put("caste", rs.getString("caste").trim());
                hm.put("working_at", rs.getString("working_at").trim());
                hm.put("email_id", rs.getString("email_id").trim());
                viewCandidate.put(rs.getString("first_name"), hm);
            }
        } catch (SQLException ex) {
            Logger.getLogger(CandidateDetails.class.getName()).log(Level.SEVERE, null, ex);
        }
        return viewCandidate;
    }

    @Override
    public Boolean registerAccount(String username, String email, String pass, String fname, String mname, String lname, String gender, String birthdate, String maritalStatus, String height, String country, String city, String contactNo, String motherTongue, String religion, String caste) {
        ConnectDB connDB = new ConnectDB();
        try {
            conn = connDB.getConnection();
            String registerSql = "insert into Candidate(username,email_id,password,first_name,middle_name,last_name,gender,birthdate,marital_status,height,country_living_in,city,contact_number,mother_tongue,religion,caste) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
            PreparedStatement pstm = conn.prepareStatement(registerSql);
            pstm.setString(1, username);
            pstm.setString(2, email);
            pstm.setString(3, pass);
            pstm.setString(4, fname);
            pstm.setString(5, mname);
            pstm.setString(6, lname);
            pstm.setString(7, gender);
            pstm.setString(8, birthdate);
            pstm.setString(9, maritalStatus);
            pstm.setString(10, height);
            pstm.setString(11, country);
            pstm.setString(12, city);
            pstm.setString(13, contactNo);
            pstm.setString(14, motherTongue);
            pstm.setString(15, religion);
            pstm.setString(16, caste);           
            pstm.execute();
            return true;            
        } catch (SQLException ex) {
            Logger.getLogger(CandidateDetails.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try {
                conn.close();
            } catch (SQLException e) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, e);
            }
        }
        return false;
    }

    @Override
    public Boolean checkEmail(String email) {
        ConnectDB connDB = new ConnectDB();
        try {
            conn = connDB.getConnection();
            String emailSql = "select email_id from Candidate where email_id = ?";
            PreparedStatement pstm = conn.prepareCall(emailSql);
            pstm.setString(1, email);
            ResultSet rs = pstm.executeQuery();
            if (rs.next()) {
                return false;
            }

            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(CandidateDetails.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

    @Override
    public Boolean checkUser(String username) {
        ConnectDB connDB = new ConnectDB();
        try {
            conn = connDB.getConnection();
            String userSql = "select username from Candidate where username = ?";
            PreparedStatement pstm = conn.prepareCall(userSql);
            pstm.setString(1, username);
            ResultSet rs = pstm.executeQuery();
            if (rs.next()) {
                return false;
            }

            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(CandidateDetails.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

    @Override
    public Boolean checkUserActivated(String username) {
        try {
            ConnectDB connDB = new ConnectDB();
            conn = connDB.getConnection();
            String checkSql = "select * from Registration where username = ? and status = 'true'";
            PreparedStatement pstm = conn.prepareStatement(checkSql);
            pstm.setString(1, username);
            ResultSet rs = pstm.executeQuery();
            if (rs.next()) {
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(CandidateDetails.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    @Override
    public Boolean registerCharges(int chargesID, String username, String date, String cardNo, String cardType, String status) {
        try {
            ConnectDB connDB = new ConnectDB();
            conn = connDB.getConnection();
            String sql = "insert into Registration(id_charges, username, date_registration, card_number, card_types, status) values(?,?,?,?,?,?)";
            PreparedStatement pstm = conn.prepareStatement(sql);
            pstm.setInt(1, chargesID);
            pstm.setString(2, username);
            pstm.setString(3, date);
            pstm.setString(4, cardNo);
            pstm.setString(5, cardType);
            pstm.setString(6, status);
            int rs = pstm.executeUpdate();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(CandidateDetails.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try {
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(CandidateDetails.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return false;
    }

    @Override
    public Boolean registerService(int serviceID, String username, String date, String cardType, String cardNo) {
        try {
            ConnectDB connDB = new ConnectDB();
            conn = connDB.getConnection();
            String sql = "insert into ServicesDetails(id_services, username, date, card_types, card_number) values(?,?,?,?,?)";
            PreparedStatement pstm = conn.prepareStatement(sql);
            pstm.setInt(1, serviceID);
            pstm.setString(2, username);
            pstm.setString(3, date);
            pstm.setString(4, cardType);
            pstm.setString(5, cardNo);
            pstm.executeQuery();
        } catch (SQLException ex) {
            Logger.getLogger(CandidateDetails.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

    @Override
    public HashMap viewCharges() {
        HashMap charges = new HashMap();
        try {
            ConnectDB connDB = new ConnectDB();
            conn = connDB.getConnection();
            String sql = "select * from Charges";
            PreparedStatement pstm = conn.prepareStatement(sql);
            ResultSet rs = pstm.executeQuery();
            while (rs.next()) {
                HashMap hm = new HashMap();
                hm.put("id_charges", rs.getInt("id_charges"));
                hm.put("duration", rs.getString("duration").trim());
                hm.put("price", rs.getInt("price"));
                charges.put(rs.getInt("id_charges"), hm);
            }

        } catch (SQLException ex) {
            Logger.getLogger(CandidateDetails.class.getName()).log(Level.SEVERE, null, ex);
        }
        return charges;
    }

    @Override
    public HashMap viewServices() {
        HashMap services = new HashMap();
        try {
            ConnectDB connDB = new ConnectDB();
            conn = connDB.getConnection();
            String sql = "select * from Services";
            PreparedStatement pstm = conn.prepareStatement(sql);
            ResultSet rs = pstm.executeQuery();
            while (rs.next()) {
                HashMap hm = new HashMap();
                hm.put("id_services", rs.getInt("id_services"));
                hm.put("select_edition", rs.getString("select_edition").trim());
                hm.put("post_your", rs.getString("post_your").trim());
                hm.put("payment", rs.getInt("payment"));
                services.put(rs.getInt("id_services"), hm);
            }

        } catch (SQLException ex) {
            Logger.getLogger(CandidateDetails.class.getName()).log(Level.SEVERE, null, ex);
        }
        return services;
    }
    @Override
    public void sendMailOK(String from, String pass, String to, String subject, String content){
        String host = "smtp.gmail.com";

        Properties props  = System.getProperties();
        props.put("mail.smtps.auth", "true");
        props.put("mail.smtps.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "587");

        Session session = Session.getDefaultInstance(props,null);
        MimeMessage message = new MimeMessage(session);

        try {
                message.setFrom(new InternetAddress(from));
                message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
                message.setSubject(subject);
                message.setText(content);

                Transport transport = session.getTransport("smtps");
                transport.connect(host,from, pass);
                transport.sendMessage(message, message.getAllRecipients());
                transport.close();

                //JOptionPane.showMessageDialog(null, "An email was sent!");
            } catch (MessagingException ex) {
                //JOptionPane.showMessageDialog(null, "Error in sending mail");
            }
    }  
}
